package com.duowei.diandan.app;

import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.os.StrictMode;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.duowei.diandan.constant.Settings;
import com.duowei.diandan.util.NetUtils;

public class App extends Application {
    private static final String TAG = "App";

    private static App sApp;

    private MyActivityLifecycleCallbacks mLifecycleCallbacks;

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sApp = this;

//        PreferenceManager.setDefaultValues(this, R.xml.pref);
        Settings.updateSettings();
        NetUtils.init();

        mLifecycleCallbacks = new MyActivityLifecycleCallbacks(() -> {
            InitManager.getInstance().stopService();
            Process.killProcess(Process.myPid());
            System.exit(0);
            Log.d(TAG, "退出App");
        });
        registerActivityLifecycleCallbacks(mLifecycleCallbacks);

        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
    }

    public static App getApp() {
        return sApp;
    }

    public MyActivityLifecycleCallbacks getLifecycleCallbacks() {
        return mLifecycleCallbacks;
    }
}
